Skip to content

olevba: don't describe _Click handlers as running on document open - #896

Open
qianeric-backup wants to merge 1 commit into
decalage2:masterfrom
qianeric-backup:olevba-click-not-on-open
Open

qianeric-backup wants to merge 1 commit into
decalage2:masterfrom
qianeric-backup:olevba-click-not-on-open

Conversation

@qianeric-backup

Copy link
Copy Markdown

What is wrong

AUTOEXEC_KEYWORDS_REGEX puts every ActiveX/control event into one group described as "Runs when the file is opened and ActiveX objects trigger events", and \w+_Click is one of them:

'Runs when the file is opened and ActiveX objects trigger events':
    (r'\w+_GotFocus', r'\w+_LostFocus', r'\w+_MouseHover', r'\w+_Click',
     r'\w+_Change', r'\w+_Resize', ...),

CommandButton1_Click fires when the user clicks the control and at no other time. A document whose only macro entry point is a button therefore gets reported as having code that runs when the file is opened — and the wording gets carried downstream, into reports and summaries, as evidence of auto-execution when no Workbook_Open / Auto_Open / Document_Open path exists at all.

Fixes #891.

What I changed

Split \w+_Click into its own entry:

# Interaction-only events. A \w+_Click handler fires when the user clicks an
# embedded control, never on document load, so it must not be described as
# running when the file is opened. Detection is unchanged (added in #499).
'Runs when the user interacts with an embedded ActiveX control (e.g. clicks a button)':
    (r'\w+_Click',),

Wording taken verbatim from your suggestion in the issue. Detection is unchanged — \w+_Click still matches and is still reported as AutoExec, exactly as #499 introduced it. Only the description differs.

Why only _Click

The rest of the group is not so clear-cut. _GotFocus, _LostFocus, _Resize and _Layout can plausibly fire while a control is initialised or rendered as a document loads, so "runs when the file is opened" is defensible for them. _Click is the one that unambiguously cannot.

Two others in the group look interaction-only to me — _Change (fires when the user edits a control's value) and _FollowHyperlink (fires on a link click) — but I left them alone rather than widening the diff beyond what the issue asked for. Say the word and I will split those too.

Tests

tests/olevba/test_autoexec.py, five cases calling detect_autoexec directly:

case checks
CommandButton1_Click gets the interaction description, and that description contains no "file is opened" / "document is opened" / "Workbook is opened"
CommandButton1_Click is still detected (guards #499's behaviour)
TextBox1_GotFocus keeps the on-open description
Workbook_Open keeps 'Runs when the Excel Workbook is opened'
both handlers in one file reported separately, each with its own description
unmodified master:     2 failed, 3 passed
with the change:       5 passed
tests/olevba/ (whole): 11 passed, 1 skipped in 2.74s

The two failures on master are the description assertion and the combined-file assertion, both showing 'Runs when the file is opened and ActiveX objects trigger events' where the interaction text is expected.

Aside

You noted in the issue that the autoexec patterns are hand-synced between olevba's keyword table and mraptor's re_autoexec regex. I checked: mraptor emits only A/W/X flags and has no _Click in re_autoexec, so it is untouched here. The drift risk is real but independent, as you said.

AUTOEXEC_KEYWORDS_REGEX puts every ActiveX/control event in one group described
as "Runs when the file is opened and ActiveX objects trigger events", including
\w+_Click. That description is wrong for _Click: CommandButton1_Click fires
when the user clicks the control and at no other time, so a document whose only
macro entry point is a button was reported as having code that runs on open.

Some of the other events in the group can plausibly fire while a control is
initialised or rendered during load (_GotFocus, _LostFocus, _Resize, _Layout),
so the wording is not wrong for them. Only _Click is split out.

Detection is unchanged - \w+_Click still matches and is still reported as
AutoExec, as introduced in decalage2#499. Only the description differs.

The exact wording comes from the issue's suggestion; happy to reword.

Test: tests/olevba/test_autoexec.py calls detect_autoexec directly and checks
that CommandButton1_Click gets the interaction description (and that it
contains no "file is opened" marker), that TextBox1_GotFocus and
Workbook_Open keep their on-open descriptions, and that a document containing
both a click and a GotFocus handler reports the two separately. Two of the five
cases fail on master, all five pass with the change; tests/olevba/ as a whole
gives 11 passed, 1 skipped.

Note the maintainer may want to look at _Change and _FollowHyperlink in the same
group, which are arguably interaction-only too - I left them alone to keep this
change to what the issue asked for.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

olevba: AutoExec description "Runs when the file is opened" is inaccurate for _Click (user-interaction) control events

1 participant